package com.sds.sweng.swat.miplatform.service.figures.components;

import java.util.HashMap;
import java.util.Map;

import org.dom4j.Node;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Cursors;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.MouseEvent;
import org.eclipse.draw2d.MouseListener;
import org.eclipse.draw2d.MouseMotionListener;
import org.eclipse.draw2d.geometry.Point;

import com.sds.sweng.swat.miplatform.service.graphics.ComponentUtils;
import com.sds.sweng.swat.miplatform.service.validation.ValidationCompareOperator;
import com.sds.sweng.swat.miplatform.service.validation.ValidationInfo;
import com.sds.sweng.swat.miplatform.service.validation.ValidationProperties;
import com.sds.sweng.swat.miplatform.service.validation.ValidationProperty;
import com.sds.sweng.swat.miplatform.service.validation.ValidationType;

public class TextboxComponent extends SimpleComponent implements SelectableComponent, ValidationComponent {

	protected Map<String, ValidationInfo> validationInfos;

	protected boolean isSelected;

	public TextboxComponent(Node component, Node parent) {
		super(component, parent);
	}

	protected void initComponent() {
		super.initComponent();

		setText(getId());

		validationInfos = new HashMap<String, ValidationInfo>();

		attatchEvents();
	}

	public void refresh(String serviceId) {
		setBorderColor(serviceId);
	}

	public ValidationInfo getValidationInfo(String serviceId) {
		if (validationInfos.get(serviceId) == null) {
			validationInfos.put(serviceId, new ValidationInfo(component, getId()));
		}

		return validationInfos.get(serviceId);
	}

	public ValidationProperty[] getValidationProperties(ValidationInfo validationInfo) {
		return new ValidationProperty[] {
			new ValidationProperty(ValidationProperties.COMPONENT_NAME , validationInfo.componentName),
			new ValidationProperty(ValidationProperties.ID, validationInfo.id),
			new ValidationProperty(ValidationProperties.BUSINESS_NAME, validationInfo.businessName),
			new ValidationProperty(ValidationProperties.TYPE, validationInfo.type),
			new ValidationProperty(ValidationProperties.REQUIRED, validationInfo.required),
			new ValidationProperty(ValidationProperties.REQUIRED_GROUP, validationInfo.requiredGroup),
			new ValidationProperty(ValidationProperties.MIN_LENGTH, validationInfo.minLength),
			new ValidationProperty(ValidationProperties.MAX_LENGTH, validationInfo.maxLength),
			new ValidationProperty(ValidationProperties.MIN, validationInfo.min),
			new ValidationProperty(ValidationProperties.MAX, validationInfo.max),
			new ValidationProperty(ValidationProperties.COMPARE_OP, validationInfo.compOp),
			new ValidationProperty(ValidationProperties.COMPARE_TARGET, validationInfo.compTarget)
		};
	}

	public String[] getValidationTypes() {
		return new String[] {
			ValidationType.NONE,
			ValidationType.CUSTOM,
			ValidationType.NUMBER,
			ValidationType.DATE,
			ValidationType.CURRENCY,
			ValidationType.EMAIL,
			ValidationType.SID,
			ValidationType.INTEGER,
			ValidationType.POSITIVE,
			ValidationType.NEGATIVE
		};
	}

	public String[] getValidationCompareOperators() {
		return new String[] {
			ValidationCompareOperator.NONE,
			ValidationCompareOperator.LESS,
			ValidationCompareOperator.LESS_EQUAL,
			ValidationCompareOperator.MORE,
			ValidationCompareOperator.MORE_EQUAL,
			ValidationCompareOperator.EQUAL
		};
	}

	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;

		if (isSelected) {
			setOpaque(true);
			setForegroundColor(selectedFgColor);
			setBackgroundColor(selectedBgColor);
		} else {
			setOpaque(false);
			setForegroundColor(ColorConstants.black);
			setBackgroundColor(ColorConstants.white);
		}
	}

	public boolean isSelected() {
		return isSelected;
	}

	protected void changeSelection() {
		drawer.setSelected(this);
	}

	protected void setBorderColor(String serviceId) {
		if (validationInfos.get(serviceId) == null) {
			setBorder(new LineBorder(ColorConstants.black, 1));
			return;
		}

		if (validationInfos.get(serviceId).partied) {
			if (validationInfos.get(serviceId).required) {
				setBorder(new LineBorder(ColorConstants.red, 2));
			} else {
				setBorder(new LineBorder(ColorConstants.blue, 2));
			}
		} else {
			setBorder(new LineBorder(ColorConstants.black, 1));
		}
	}

	protected void attatchEvents() {
		addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent me) {
				switch (me.button) {
					case 1:
						changeSelection();
						break;
					case 2:
						break;
					default:
						break;
				}
			}

			public void mousePressed(MouseEvent me) {
				// Ignore
			}

			public void mouseDoubleClicked(MouseEvent me) {
				// Ignore
			}
		});

		addMouseMotionListener(new MouseMotionListener() {

			public void mouseMoved(MouseEvent me) {
				// Ignore
			}

			public void mouseHover(MouseEvent me) {
				// Ignore
			}

			public void mouseExited(MouseEvent me) {
				setCursor(Cursors.ARROW);
			}

			public void mouseEntered(MouseEvent me) {
				setCursor(Cursors.HAND);
			}

			public void mouseDragged(MouseEvent me) {
				// Ignore
			}
		});
	}

	protected void paintFigure(Graphics graphics) {
		super.paintFigure(graphics);
	}

	public Point getComponentLocation() {
		int x = ComponentUtils.getComponentBound(component, parent).x;
		int y = ComponentUtils.getComponentBound(component, parent).y;

		return new Point(x, y);
	}

}
